package com.ptszyxx.popose.module.base.user.vm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ptszyxx.popose.common.utils.YBusUtil;
import com.ptszyxx.popose.module.main.user.vm.UserEditLabelVM;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.http.data.entity.user.LabelItemEntity;
import com.ysg.http.data.entity.user.UserLabelResult;
import com.ysg.utils.YSPUtils;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserTagVM extends BaseViewModel<CommonRepository> {
    public List<LabelItemEntity> tagList = new ArrayList<>();
    public String ids;

    public UserTagVM(@NonNull Application application, CommonRepository repository) {
        super(application, repository);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onRefreshEvent = new SingleLiveEvent<>();
    }


    public BindingCommand onSubmitCommand = new BindingCommand(() -> requestUserUpdate());

    /**
     */
    public void requestList(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .data(model.userEditLabelList(map), this,
                        new OnSuccessResult<UserLabelResult>() {
                            @Override
                            public void onSuccessResult(BaseResponse<UserLabelResult> response) {
                                tagList.addAll(response.getData().getGexing());
                                tagList.addAll(response.getData().getXingqu());
                                uc.onRefreshEvent.setValue(response.getData());
                            }
                        });
    }

    public void requestUserUpdate() {
        if (YStringUtil.isEmpty(ids)){
            YToastUtil.showShort("请选择标签");
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("biaoqian", ids);
        HttpUtils.getInstance()
                .data(model.userEditLabel(map), this,
                        new OnSuccessResult<UserLabelResult>() {
                            @Override
                            public void onSuccessResult(BaseResponse<UserLabelResult> response) {
                                YToastUtil.showShort(R.string.success);
                                YActivityUtil.getInstance().jumpUserResult(UserTagVM.this);
                            }
                        });
    }


}
